/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.utils
{
	import funit.core.RunState;
	import funit.core.TestResult;
	import funit.core.TestSuite;
	
	public class ResultSummarizer
	{
		private var _name:String;
		private var _time:Number;
		
		private var _resultCount:int;
		private var _failureCount:int;
		private var _skipCount:int;
		private var _ignoreCount:int;
		private var _suitesNotRun:int;
		
		
		/**
		 * @param result
		 */
		public function ResultSummarizer( result:TestResult )
		{
			_name = result.name;
			_time = result.time;
			
			summarize(result);
		}
		
		
		/**
		 * @param result
		 */
		private function summarize( result:TestResult ) : void
		{
			if (result.test is TestSuite)
			{
				summarizeSuiteResult(result);
			}
			else
			{
				summarizeCaseResult(result);
			}
		}
		
		/**
		 * @param suiteResult
		 */
		private function summarizeSuiteResult( suiteResult:TestResult ) : void
		{
			for each ( var result:TestResult in suiteResult.results )
			{
				summarize(result);
			}
			
			if (!suiteResult.executed)
			{
				_suitesNotRun++;
			}
		}
		
		/**
		 * @param caseResult
		 */
		private function summarizeCaseResult( caseResult:TestResult ) : void
		{
			switch (caseResult.runState)
			{
				case RunState.Executed:
					_resultCount++;
					if (caseResult.isFailure)
					{
						_failureCount++;
					}
					break;
				case RunState.Ignored:
					_ignoreCount++;
					break;
				case RunState.Explicit:
				case RunState.NotRunnable:
				case RunState.Runnable:
				case RunState.Skipped:
				default:
					_skipCount++;
					break;
			}
		}
		
		
		/**
		 * 
		 */
		public function get name() : String
		{
			return _name;
		}
		
		
		/**
		 * 
		 */
		public function get time() : int
		{
			return _time;
		}
		
		
		/**
		 * 
		 */
		public function get isSuccess() : Boolean
		{
			return (_failureCount == 0);
		}
		
		
		/**
		 * 
		 */
		public function get resultCount() : int
		{
			return _resultCount;
		}
		
		
		/**
		 * 
		 */
		public function get failureCount() : int
		{
			return _failureCount;
		}
		
		
		/**
		 * 
		 */
		public function get skipCount() : int
		{
			return _skipCount;
		}
		
		
		/**
		 * 
		 */
		public function get ignoreCount() : int
		{
			return _ignoreCount;
		}
		
		
		/**
		 * 
		 */
		public function get testsNotRun() : int
		{
			return (_skipCount + _ignoreCount);
		}
		
		
		/**
		 * 
		 */
		public function get suitesNotRun() : int
		{
			return _suitesNotRun;
		}
		
	}
	
}
